/**
 * @modal: 智能拆分策略
 */
import CommonTable from "@/components/CommonTable";
import { Button, Switch, Popover } from "antd";
import { QuestionCircleOutlined, EditOutlined } from '@ant-design/icons';
import OrderScope from './OrderScope';
import { useState, useRef } from "react";
import { useNavigate } from "react-router-dom";
import { find } from "lodash";

const strategyType = {
	1: "待审核+异常",
	2: "筛选的订单",
	3: "快捷查询池",
};

function StrategyTable(props) {

	//策略列表
	const { type, strategyData, saveData, handleOpenModal } = props;

	const { smartStrategyConfigs } = strategyData;
	//订单范围弹框设置
	const [open, setOpen] = useState(false);

	//当前编辑的行信息
	const [item, setItem] = useState({});

	const navigate = useNavigate();

    const childRef = useRef();

	const strategyColumns = [
		{
			title: "优先级",
			dataIndex: "level",
			render: t => {
				return t;
			}
		},
		{
			title: "智能策略标题",
			dataIndex: "title",
			render: t => {
				return t;
			}
		},
		{
			title: (
				<div>
					<span style={{ marginRight: '8px' }}>订单范围</span>
					<Popover content={<div style={{ width: '160px' }}>筛选的订单：只会对其中待审核、异常的订单进行处理。其中拆单只针对待审核订单</div>} >
						<QuestionCircleOutlined />
  				</Popover>
				</div>
			),
			dataIndex: "scopeType",
			render: (t, r) => {
				return (
					<>
						<span>{t == 1 && !r.orderStatusList.includes(30) ? '待审核' : strategyType[t]}</span>
						<EditOutlined onClick={() => editScopeType(r)} style={{ color: '#108ee9', cursor: 'pointer', marginLeft:'4px'}} />
                        <div>{r.queryName && `(${r.queryName})`}</div>
					</>
				) 
			}
		},
		{
			title: "是否启用",
			dataIndex: "status",
			render: (t, r) => {
				return <Switch checkedChildren="已开启" unCheckedChildren="已关闭" defaultChecked={t} onChange={(e) => changeStatus(e, r)} />
			}
		},
		{
			title: "操作",
			dataIndex: "code",
			render: (t, r) => {
				return <Button type="link" onClick={() => handleOpenPage(t)}>去设置</Button>;
			}
		},
	];

	//跳转数据
	const handleOpenPage = (val) => {
        if(val == 'smart-split') {
            handleOpenModal()
        } else {
            const openConfig = {
                'smart-label': '/views/order/rule?tab1=1',
                // 'smart-split': '/views/order/list?modalName=autoSplitOrder',
                'smart-merge': '/views/order/rule?tab1=0&tab2=2',
                'smart-packet': '/views/order/rule?tab1=2',
                'smart-logistics': '/views/order/rule?tab1=3',
                'auto-audit': '/views/order/rule?tab1=4',
            }
            const url = openConfig[val];
            navigate(url);
        }
	}

	//点击修改订单范围
	const editScopeType = r => {
		setItem(r);
		setOpen(true);
	}

	//更改启用状态
	const changeStatus = (val, r) => {
		if(val) {
			r.status = 1;
		} else {
			r.status = 0;
		}
		saveData(smartStrategyConfigs);
	}

	const TableProps = {
		rowKey: "code",
		isSortData: true,
		data: smartStrategyConfigs,
		columns: strategyColumns,
		onDragEnd(val) {
            val.map((item, index) => item.level = index + 1);
			saveData(val);
		}
	};

	//修改订单范围
	const OrderScopeProps = {
		type,
		item,
		handleOk(val) {
			item.scopeType = val.scopeType;
			item.queryId = val.queryId;
            if(childRef.current && childRef.current.queryList && childRef.current.queryList.length) {
                const z = find(childRef.current.queryList, ['id', val.queryId]);
                item.queryName = z ? z.name: null;
            }
			saveData(smartStrategyConfigs);
			setOpen(false);

		},
		handleCancel() {
			setOpen(false);
		}
	}

	return (
		<>
			<CommonTable {...TableProps} />
			{open && <OrderScope ref={childRef} { ...OrderScopeProps } />}
		</>
	);
}

export default StrategyTable;
